//
//  B_LeaveListVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/9/5.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_LeaveListVC.h"
#import "B_SubmitFooterView.h"
#import "B_LeavelMessageListTVC.h"
#import "B_LeaveMessageHeaderView.h"
#import "B_LeaveListModel.h"
#import "B_LeaveMessagesVC.h"

@interface B_LeaveListVC ()

@property (nonatomic, strong) BaseView * mainView;

@property (nonatomic, strong) B_SubmitFooterView * submitView;

@end

@implementation B_LeaveListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.view = self.mainView;
    [self getData:YES];
    
}
- (BaseView *)mainView {
    WeakSelf(weSelf, self);
    if (!_mainView) {
        _mainView = [[BaseView alloc] init];
        [_mainView addSubview:self.submitView];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (49 + 24) : 49;
        [self.submitView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(bottom));
        }];
        
        [_mainView setTableViewFrameWithTop:-getNavHeight() left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_LeavelMessageListTVC" bundle:nil] forCellReuseIdentifier:@"B_LeavelMessageListTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return weSelf.dataArray.count;
        };
        
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            
            B_LeaveMessageHeaderView * header = [B_LeaveMessageHeaderView initB_LeaveMessageHeaderView];
            B_LeaveListModel * model = weSelf.dataArray[section];
            [header.B_imageView sd_setImageWithURL:[NSURL URLWithString:model.portrait]];
            header.B_userName.text = model.nickName;
            header.B_contentLabel.text = model.content;
            return nil;
        };
        
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return 2;
        };
        
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_LeaveListModel * model = weSelf.dataArray[indexPath.section];
            B_LeavelMessageListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_LeavelMessageListTVC" forIndexPath:indexPath];
            cell.B_imageView.backgroundColor = [UIColor redColor];
            cell.B_userLabel.text = @"小姐姐";
            cell.B_contentLabel.text = @"今晚没空";
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            
        };
        
        
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            return nil;
        };
        
        [_mainView setTableViewHeaderFresh:^{
            [weSelf getData:YES];
        }];
        [_mainView setTableViewFooterFresh:^{
            [weSelf getData:NO];
        }];
        
    }
    return _mainView;
}

- (B_SubmitFooterView *)submitView {
    WeakSelf(weSelf, self);
    if (!_submitView) {
        _submitView = [B_SubmitFooterView initB_SubmitFooterView];
        [_submitView.B_SubmitButton setTitle:@"我要留言" forState:UIControlStateNormal];
        _submitView.B_SubmitButton.backgroundColor = [UIColor color_pink];
        _submitView.submitAction = ^{
            //TODO:执行提交操作
            B_LeaveMessagesVC * leave = [[B_LeaveMessagesVC alloc] init];
            leave.B_userId = weSelf.B_userId;
            [weSelf pushNextViewController:leave];
            
        };
    }
    return _submitView;
}

- (void) getData:(BOOL) isRefresh {
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index ++;
    }
    NSString * stringPath;
    if (self.type == B_LeaveListVCTypeFromMessage) {
        stringPath = [NSString stringWithFormat:@"/yearnfor/inter/userLeaveWord/selectUserLeaveWord/%@/%@/%ld/10", self.B_userId, getUserId(), self.index];
    } else if (self.type == B_LeaveListVCTypeFromMainPage) {
        stringPath = [NSString stringWithFormat:@"/yearnfor/inter/userLeaveWord/selectUserLeaveWord/%@/%@/%ld/10", getUserId(), self.B_userId, self.index];
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:stringPath parameters:nil isCache:isRefresh succeed:^(id data) {
        if (isRefresh) {
            [weSelf.dataArray removeAllObjects];
        }
        if ([data[@"code"] isEqualToString:@"20010"]) {
            NSLog(@"data is %@", data);
            for (NSDictionary * dictionary in data[@"values"]) {
                B_LeaveListModel * model = [B_LeaveListModel mj_objectWithKeyValues:dictionary];
                [weSelf.dataArray addObject:model];
            }
            [weSelf.mainView endRefresh];
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
